package com.adz.udpdemo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MainActivity extends Activity {
    Button btnServer;
    Button btnClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnServer= (Button) findViewById(R.id.btn_server);
        btnClient= (Button) findViewById(R.id.btn_client);
        btnServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSever();
            }
        });
        btnClient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setClient();
            }
        });

    }

    public void setClient(){
        final String request="M-SEARCH * HTTP/1.1\nHOST: 255.255.255.255:1901\nMAN: \"nsdp:discover\"\nST: nsdp:all\nMX: 3\nSN: 79\r\n\r\n";
        new Thread(new Runnable() {
            @Override
            public void run() {
        /*
		 * 向服务器端发送数据
		 */
        //1.定义服务器的地址、端口号、数据
        InetAddress address= null;
        try {
            address = InetAddress.getByName("255.255.255.255");
            int port=1901;
            byte[] data=request.getBytes();
            //2.创建数据报，包含发送的数据信息
            DatagramPacket packet=new DatagramPacket(data, data.length, address, port);
            //3.创建DatagramSocket对象
            DatagramSocket socket=new DatagramSocket();
            //4.向服务器端发送数据报
            socket.send(packet);

		/*
		 * 接收服务器端响应的数据
		 */
            //1.创建数据报，用于接收服务器端响应的数据
            byte[] data2=new byte[1024];
            Log.d("MainActivity","*************");
            DatagramPacket packet2=new DatagramPacket(data2, data2.length);
            Log.d("MainActivity","&&&&&&&&&&&&&");
            //2.接收服务器响应的数据
            socket.receive(packet2);
            Log.d("MainActivity","#############");
            //3.读取数据
            String reply=new String(data2, 0, packet2.getLength());
            Log.d("MainActivity","我是客户端，服务器说："+reply);
            //4.关闭资源
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
            }
        }).start();
    }


     public void setSever(){
         new Thread(new Runnable() {
             @Override
             public void run() {
       /*
		 * 接收客户端发送的数据
		 */
         //1.创建服务器端DatagramSocket，指定端口
         DatagramSocket socket= null;
         try {
             socket = new DatagramSocket(1901);
             //2.创建数据报，用于接收客户端发送的数据
             byte[] data =new byte[1024];//创建字节数组，指定接收的数据包的大小
             DatagramPacket packet=new DatagramPacket(data, data.length);
             //3.接收客户端发送的数据
             Log.d("MainActivity","****服务器端已经启动，等待客户端发送数据");
             socket.receive(packet);//此方法在接收到数据报之前会一直阻塞
             //4.读取数据
             String info=new String(data, 0, packet.getLength());
             Log.d("MainActivity",info);
		/*
		 * 向客户端响应数据
		 */
             //1.定义客户端的地址、端口号、数据
             InetAddress address=packet.getAddress();
             int port=packet.getPort();
             byte[] data2="欢迎您!".getBytes();
             //2.创建数据报，包含响应的数据信息
             DatagramPacket packet2=new DatagramPacket(data2, data2.length, address, port);
             //3.响应客户端
             socket.send(packet2);
             //4.关闭资源
             socket.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
             }
         }).start();

     }






}
